package com.example.filetransfer.util;

import java.io.*;
import java.util.Base64;

public class Base64Util {
    
    public static String encodeFile(File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] bytes = new byte[(int) file.length()];
            fis.read(bytes);
            return Base64.getEncoder().encodeToString(bytes);
        }
    }

    public static void decodeToFile(String base64String, File outputFile) throws IOException {
        byte[] decodedBytes = Base64.getDecoder().decode(base64String);
        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            fos.write(decodedBytes);
        }
    }

    public static String encodeString(String text) {
        return Base64.getEncoder().encodeToString(text.getBytes());
    }

    public static String decodeString(String base64String) {
        byte[] decodedBytes = Base64.getDecoder().decode(base64String);
        return new String(decodedBytes);
    }
} 